﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.GlobalVariables;

public partial class Login : System.Web.UI.Page
{
    private UserManageDAO userDao = new UserManageDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        //OperationFile.ReNameFile("ZWDB1000-2003-01-027.PDF", "ZWDB1000-2003-01-028.PDF");
        /*
        User user = new User();
        user.name = "admin";
        user.password = "123";
        user.role = UserRole.ADMIN;
        user.grantedModulars = new List<FunctionModular>();
        FunctionModular f = new FunctionModular();
        f.id = "dianlibaokan";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "guodianfagui";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "guojiabiaozhun";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "guojibiaozhun";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "guoyanbaogao";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "hangyebiaozhun";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "jingjibaokan";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "kaochabaogao";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "kejibaogao";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "kejichengguo";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "nengyuanbaogao";
        user.grantedModulars.Add(f);
        

        f = new FunctionModular();
        f.id = "tushushouce";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "waiwenhuiyi";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "waiwenqikan";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "xueweilunwen";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "zhongguochengguo";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "zhongwenhuiyi";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "zhongwenqikan";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "zichanwenxian";
        user.grantedModulars.Add(f);

        f = new FunctionModular();
        f.id = "zonghebaokan";
        user.grantedModulars.Add(f);

        userDao.UpdateUserByName(user);
         * */
    }
    protected void submitBtn_Click(object sender, EventArgs e)
    {
        if ("".Equals(this.unameTextBox.Text.Trim()))
        {
            Response.Write("<Script>alert('用户名不能为空')</Script>");
            return;
        }

        if ("".Equals(this.upwdTextBox.Text.Trim()))
        {
            Response.Write("<Script>alert('密码不能为空')</Script>");
            return;
        }

        User user=userDao.GetUserByName(this.unameTextBox.Text.Trim());
        if (null == user)
        {
            return;
        }

        if (this.upwdTextBox.Text.Trim().Equals(user.password))
        {
            String ipAddress = Request.UserHostAddress;
            if (ipAddress.Equals(user.ipLimit))
            {
                Session.Add("login", user);
            }
            else
            {
                Response.Write("<Script>alert('您登陆的IP(" + ipAddress + ")受限制')</Script>");
                return;
            }     
        }
        else
        {
            Response.Write("<Script>alert('用户名或密码错误')</Script>");
            return;
        }
        Response.Redirect(GlobalResource.INDEX_PAGE);
    }
}